<?php


namespace Gateway;

/**
 * 一砂云网关
 */
class ESGateway
{
    /**
     * 网关配置的算法
     *  MD5("3"),
        ECC256("1"),
        RSA2048("2");
     */
    public $algo;
    /**
     * 网关密钥
     */
    public $key;
    /**
     * 用户的APPCODE
     */
    public $appCode;

    /**
     * 实例化
     * @param appCode 用户appCode,参考 : https://esandinfo.yuque.com/yv6e1k/aa4qsg/cdwove
     * @param algo 网关配置的算法
                    MD5("3"),
                    ECC256("1"),
                    RSA2048("2");
     * @param key 网关密钥
     */
    public function __construct($appCode, $algo, $key) {
        $this->appCode = $appCode;
        $this->algo = $algo;
        $this->key = $key;
    }


    /**
     * @param act           业务action（参考具体业务协议文档）
     * @param bizContent    业务数据，（参考具体业务协议文档）(json字符串)
     * @return
     */
    public function sendToGateWay ($act, $bizContent) {
        $gatewayResponse = new GatewayResponse();
        do {
            // 对传入参数进行判断
            if ($act == null || $bizContent == null) {
                $gatewayResponse->initWithCode("-1", "无效传入参数");
                break;
            }

            // 构造请求报文
            $gatewayRequest = new GatewayRequest();
            $gatewayRequest->act = $act;
            $gatewayRequest->appCode = $this->appCode;
            $gatewayRequest->bizNo = CommUtil::getTimeUUID();
            $gatewayRequest->type = $this->algo;
            $gatewayRequest->timestamp = CommUtil::getCurrentTimeStr();
            $gatewayRequest->bizContent = $bizContent;

            // 对数据进行签名
            $signResult = $gatewayRequest->genSign($this->key);
            if (!$signResult) {
                $gatewayResponse->initWithCode("-1", "执行签名失败");
                break;
            }

            // 发送请求到网关
            //域名后、query前的部分
            $host = "https://edis.esandcloud.com";
            $path = "/gateways";
            $request = new HttpRequest($host, $path, HttpMethod::POST, "", "");
            //设定Content-Type，根据服务器端接受的值来设置
            $request->setHeader(HttpHeader::HTTP_HEADER_CONTENT_TYPE, ContentType::CONTENT_TYPE_JSON);
            //设定Accept，根据服务器端接受的值来设置
            $request->setHeader(HttpHeader::HTTP_HEADER_ACCEPT, ContentType::CONTENT_TYPE_JSON);
            //注意：业务body部分，不能设置key值，只能有value
            $request->setBodyString($gatewayRequest->toJsonStr());
            LOG::DEBUG("发送报文：".$gatewayRequest->toJsonStr());
            $httpResponse = HttpClient::execute($request);
            $statusCode = $httpResponse->httpStatusCode;
            if ($statusCode != '200') {
                LOG::ERROR("服务器处理异常, 状态码 : ". $statusCode);
                $gatewayResponse->initWithCode("-1", "服务处理异常");
            }

            $rspBody = $httpResponse->getBody();
            LOG::DEBUG("服务器响应：".$rspBody);
            if ($rspBody != null) {
                $gatewayResponse = GatewayResponse::fromJsonStr($rspBody);
                if ("0000" == $gatewayResponse->code) {
                    $signVerifyResult = $gatewayResponse->verifySign($this->key);
                    if ($signVerifyResult) {
                        Log::WARN("验签通过");
                    } else {
                        Log::WARN("验签不通过");
                    }
                }
            }
        } while (false);

        return $gatewayResponse;
    }
}
